package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.ListSelectionModel;

import controller.SelectGlobalEntityController;

public class SelectGlobalEntityView extends View<SelectGlobalEntityController> implements Displayable{

	private JFrame frame;
	private SelectGlobalEntityController controller;
	@SuppressWarnings("rawtypes")
	JList list;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public SelectGlobalEntityView(List<String> entities) {
		String s[] = new String[entities.size()];
		Integer i = 0;
		for (String string : entities) {
			s[i] = string;
			i++;
		}
		list = new JList(s);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame("Seleccionar Entidad");
		frame.setBounds(100, 100, 450, 300);
		frame.getContentPane().setLayout(null);
		
		list.setBounds(149, 29, 132, 172);
		frame.getContentPane().add(list);
		
		JButton btnTerminar = new JButton("Terminar");
		btnTerminar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				getController().handleButtonTerminar();
			}
		});
		btnTerminar.setBounds(159, 213, 117, 25);
		frame.getContentPane().add(btnTerminar);
	}

	@Override
	public void show() {
		frame.setVisible(true);
	}

	@Override
	public void hide() {
		frame.setVisible(false);		
	}

	@Override
	public SelectGlobalEntityController getController() {
		return this.controller;
	}

	@Override
	public void setController(SelectGlobalEntityController controller) {
		this.controller = controller;
	}

	public String getSelectedEntity(){
		if (this.list.getSelectedValue() != null){
			return this.list.getSelectedValue().toString();
		}
		return null;
	}
	
}
